﻿#region Using Statements
using System;
using System.Diagnostics;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Input;
#endregion

namespace GameStateManagement
{
    class Player
    {
        static float BOOSTPOWER_MAX = 500;
        static float SHIELDPOWER_MAX = 250; 
        public List<Shield> shieldList = new List<Shield>();
        protected int score = 0;
        public Vector2 position;
        protected float rotation = 0; 
        protected Vector2 speed = Vector2.Zero;
        KeyboardState keyboardState;
        protected float shieldPower;
        protected float boostPower;
        protected bool shieldsLocked = false;
        protected bool boostersLocked = false; 
        int count = 12;
        Texture2D shield2; 

        public Player()
        {
            position.X = 5;
            position.Y = 5;
            shieldPower = SHIELDPOWER_MAX;
            boostPower = BOOSTPOWER_MAX;
            
        }

        public Vector2 direction
        {
            get
                  {
                    Vector2 dir = Vector2.Zero; 
                 
                

                    if (keyboardState.IsKeyDown(Keys.Left) || GamePad.GetState(PlayerIndex.One).DPad.Left== ButtonState.Pressed) rotation -= 0.05f;
                    if (keyboardState.IsKeyDown(Keys.Right) || GamePad.GetState(PlayerIndex.One).DPad.Right == ButtonState.Pressed) rotation += 0.05f;
                    if (keyboardState.IsKeyDown(Keys.Up) || GamePad.GetState(PlayerIndex.One).DPad.Up == ButtonState.Pressed)
                        {
                        if (keyboardState.IsKeyDown(Keys.Space) || GamePad.GetState(PlayerIndex.One).Buttons.X == ButtonState.Pressed)
                            speed = new Vector2(2f);
                       
                        else if (keyboardState.IsKeyDown(Keys.S) || GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed)
                        {
                            if(boostPower > 0 && !boostersLocked)
                            {
                                speed = new Vector2(5f);
                                boostPower -= 5; 
                            }
                            

                            if (boostPower < 0)
                            {
                                boostPower = 0;
                                boostersLocked = true;
                            }

                            if (boostPower < 1)
                                speed = new Vector2(3f);
                        }


                         
                           
                               
                               
                        
                        else
                            speed = new Vector2(3f);

                        dir = new Vector2((float)Math.Sin(rotation), -(float)Math.Cos(rotation));
                                    
                        }
                    
                   return dir * speed;
                }
                
        }

        public int getScore(){return score;}
        public float getRot() { return rotation; }
        public float getShields() { return shieldPower; }
        public float getBoost() { return boostPower; } 
        public void setRot(float rot)
        {
            rotation = rot;
        }

        
        public void update(GameTime gameTime)
        {
            keyboardState = Keyboard.GetState(); 
            Vector2 prevPosition = position;

            if (keyboardState.IsKeyDown(Keys.Space) || GamePad.GetState(PlayerIndex.One).Buttons.X == ButtonState.Pressed)
            {
                if (shieldPower > 0 && !shieldsLocked)
                {
                    addShield();
                    shieldPower -= 5;
                     

                    if (shieldList.Count > 50)
                        shieldList.RemoveAt(0);
                    if (shieldPower < 1)
                        shieldsLocked = true; 
                    if (shieldPower < 0)
                        shieldPower = 0;
                }
            }
            else if (shieldPower < SHIELDPOWER_MAX)
            {
                shieldPower += 1;
                if (shieldPower >= SHIELDPOWER_MAX / 2)
                    shieldsLocked = false;
            }
            if (shieldList.Count > 0) 
            {
                foreach (Shield s in shieldList)
                    s.Update(gameTime); 

                count --; 
                if(count == 0){
                    shieldList.RemoveAt(0);
                    count = 12;
                }
            }


            if (boostPower < BOOSTPOWER_MAX)
            {
                if (keyboardState.IsKeyUp(Keys.S) && GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Released)
                    boostPower += 1f;
                if (boostPower >= BOOSTPOWER_MAX / 2)
                    boostersLocked = false; 
            }                   

           

            position += direction;

           //if (position.X < 0 || position.X > ScreenManager.GraphicsDevice.Viewport.Width || position.Y < 0 || position.Y > graphics.GraphicsDevice.Viewport.Height)
             //   position = prevPosition;

        }

        public void addShield()
        {
            shieldList.Add(new Shield(position, rotation, shield2));

        }

        public void setShield(Texture2D shield)
        {
            this.shield2 = shield;
        }


    }
}
